# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2

inherit alice

DESCRIPTION="C++ data analysis framework and interpreter from CERN"
SRC_URI=""
ESVN_PROJECT="root"
HOMEPAGE="http://root.cern.ch/"

SLOT="${PV}"
LICENSE="LGPL-2.1"
IUSE="+alien +math +monalisa +fftw3"
INS_DIR="/opt/cern/alice/root"

# PYTHON_DEPEND="3:3.2"

CDEPEND="dev-libs/libpcre
  media-libs/libpng
  virtual/jpeg
  media-libs/giflib
  media-libs/tiff
  sys-apps/shadow
  virtual/opengl
  media-libs/ftgl
  virtual/glu
  x11-libs/libXpm
  x11-libs/libXft
  dev-libs/openssl
  dev-libs/libxml2
  media-libs/libafterimage
  =dev-libs/openssl-alien-0.9.8q-r1
  fftw3? ( sci-libs/fftw )
  monalisa? ( sci-physics/ApMon_cpp )
  alien? ( ~sci-physics/xrootd-$XROOTD_VERSION ~sci-physics/alien-xalienfs-$ALIEN_VERSION )"

DEPEND="${CDEPEND}
  dev-util/pkgconfig
  >=sci-physics/eselect-alien-1.1.0
  >=sci-physics/eselect-root-1.1.0"

RDEPEND="${CDEPEND}"

S="${WORKDIR}/${PN}"

XRD_OPENSSL_DIR="/opt/cern/alice/openssl-alien-0.9.8q"

pkg_setup() {

  export EXTRA_ECONF_ALICE;
  EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --with-pythia6-uscore=SINGLE --with-f77=gfortran"

  if use alien ; then
    einfo "AliEn is enabled"
    alice_set_alien_env
    EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --with-alien-incdir=$ALIEN_DIR/include --with-alien-libdir=$ALIEN_DIR/lib"
    EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --with-ssl-incdir=$XRD_OPENSSL_DIR/include/ --with-ssl-libdir=$XRD_OPENSSL_DIR/lib"
    EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --with-xrootd-incdir=$XROOTD_DIR/include/xrootd --with-xrootd-libdir=$XROOTD_DIR/lib"
    EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --disable-globus --disable-mysql"
  fi

#   if use debug ; then
#     einfo
#     einfo "Debug is enabled"
#     einfo
#     EXTRA_ECONF_ALICE="$EXTRA_ECONF_ALICE --build=debug"
#   fi
}

src_unpack() {
  subversion_src_unpack "$@"
  cp -R ${ESVN_WC_PATH}/.svn ${S}/
}

src_configure() {

  if use x86; then
    ROOT_ARCH="linux"
  elif use amd64; then
    ROOT_ARCH="linuxx8664gcc"
  else
    die "arch is not supported"
  fi

  ./configure $ROOT_ARCH \
    --fail-on-missing \
    --prefix=${INS_DIR}/${PV} \
    --libdir=${INS_DIR}/${PV}/lib \
    --incdir=${INS_DIR}/${PV}/include \
    --docdir=${INS_DIR}/${PV}/share/doc \
    --etcdir=${INS_DIR}/${PV}/etc \
    --tutdir=${INS_DIR}/${PV}/tutorials \
    --testdir=${INS_DIR}/${PV}/test \
    --with-sys-iconpath=${INS_DIR}/${PV}/share/pixmaps \
    --disable-builtin-afterimage \
    --disable-builtin-freetype \
    --disable-builtin-ftgl \
    --disable-builtin-pcre \
    --disable-builtin-zlib \
    --disable-rpath \
    --enable-explicitlink \
    --enable-gdml \
    --enable-shadowpw \
    --enable-soversion \
    --enable-table \
    $(use_enable math gsl-shared) \
    $(use_enable math minuit2) \
    $(use_enable math roofit) \
    $(use_enable math unuran) \
    $(use_enable fftw3) \
    $(use_enable alien) \
    $(use_enable monalisa) \
    ${EXTRA_ECONF_ALICE} \
    ${EXTRA_ECONF} \
    || die "configure failed"

#

#     --with-python-libdir=/usr/lib64 --with-python-incdir=/usr/include/python3.2 \
}

# src_prepare() {
#   if [ "${PV}" == "9999" ];then
#     epatch "${FILESDIR}"/python3.patch
#   fi
# }

src_compile() {
  emake OPT="${CFLAGS}" F77OPT="${FFLAGS}" || die "emake failed"
#   make -j3 OPT="${CFLAGS}" F77OPT="${FFLAGS}" LDFLAGS="-Wl,--no-as-needed"|| die "emake failed"
}


src_install() {
  emake DESTDIR="${D}" install || die "emake install failed"
  cp -R "${S}/etc/" "${D}/${INS_DIR}/${PV}/" || die "Install failed!"
  echo "$XROOTD_VERSION" > ${D}/${INS_DIR}/${PV}//gentoo-root-deps
  echo "$ALIEN_VERSION" >> ${D}/${INS_DIR}/${PV}//gentoo-root-deps
}
